<?php
namespace App\Model;

use PhalApi\Model\NotORMModel as NotORM;

class Home extends NotORM {

	/* 轮播 */   	
    public function getSlide($where) {

		$list=\PhalApi\DI()->notorm->slide_item
				->select('image,url')
				->where($where)
                ->order('list_order asc')
				->fetchAll();
        foreach($list as $k=>$v){
            $v['image']=\App\get_upload_path($v['image']);
            $v['url']=html_entity_decode($v['url']);
            
            $list[$k]=$v;
        }
                
        return $list; 
    }
	
	/* 主播列表 */
    public function getList($where='',$p='') {
        if($p<1){
            $p=1;
        }
		$pnum=50;
		$start=($p-1)*$pnum;
        
        $list=\PhalApi\DI()->notorm->user
				->select('id,user_nickname,avatar,sex,signature,goodnums,badnums,online,isvoice,voice_value,isvideo,video_value,isdisturb,votestotal')
				->where('user_type=2 and  isauthor_auth =1')
                ->where($where)
                ->order('online desc,recommend_val desc,level_anchor desc')
                ->limit($start,$pnum)
				->fetchAll();  
		return $list;
    }

	/* 关注主播 */
    public function getAttention($uid) {

        $list=\PhalApi\DI()->notorm->user_attention
				->select('touid')
				->where('uid=?',$uid)
				->fetchAll();  
		return $list;
    }
    
	/* 附近 */
    public function getNearby($uid,$lng,$lat,$p) {
        if($p<1){
            $p=1;
        }
		$pnum=50;
		$start=($p-1)*$pnum;
        
        $list=\PhalApi\DI()->notorm->user
				->select("id,user_nickname,avatar,sex,signature,goodnums,badnums,online,isvoice,voice_value,isvideo,video_value,isdisturb,getDistance('{$lat}','{$lng}',lat,lng) as distance,votestotal")
				->where('id!=? and user_type=2 and  isauthor_auth =1 and lat!="" and lng!="" ',$uid)
                ->order('distance asc,online desc,recommend_val desc,level_anchor desc')
                ->limit($start,$pnum)
				->fetchAll();  
		return $list;
    }

	/* 搜索 */
    public function search($uid,$key,$p) {
        if($p<1){
            $p=1;
        }
		$pnum=50;
		$start=($p-1)*$pnum;
        
		$where=' user_type="2" and ( id=? or user_nickname like ? ) and id!=?';
/* 		if($p!=1){
			$id=$_SESSION['search'];
            if($id){
                $where.=" and id < {$id}";
            }
		} */
		
		$result=\PhalApi\DI()->notorm->user
				->select("id,user_nickname,avatar,sex,signature,consumption,goodnums,badnums,votestotal,isauth,isauthor_auth")
				->where($where,$key,'%'.$key.'%',$uid)
				->order("id desc")
				->limit($start,$pnum)
				->fetchAll();

		/* if($result){
			$last=end($result);
			$_SESSION['search']=$last['id'];
		} */
		
		return $result;
    }

	
	/* 最新认证主播列表 */
    public function getAuthList($p,$where="1=1") {
        if($p<1){
            $p=1;
        }
		$pnum=20;
		$start=($p-1)*$pnum;
        
        $list=\PhalApi\DI()->notorm->author_auth
				->select('uid')
				->where($where)
				->where('status=1')
                ->order('uptime desc')
                ->limit($start,$pnum)
				->fetchAll();  
		return $list;
    }
	//获取用户信息
	public function getHomeuserinfo($uid){
		$userinfo=\PhalApi\DI()->notorm->user
				->select('id,user_nickname,avatar,sex,signature,goodnums,badnums,online,isvoice,voice_value,isvideo,video_value,isdisturb,votestotal')
				->where('user_type=2 and  isauthor_auth =1 and id=?',$uid)
				->fetchOne();  
		return $userinfo;
	}
	
	//收益(魅力)榜
	public function profitList($where='',$p=''){
		 if($p<1){
            $p=1;
        }
        // 保证只能查询两页数据
        if($p>3){
            $p=100000000;
        }
		$pnum=50;
		$start=($p-1)*$pnum;
		
		$result=\PhalApi\DI()->notorm->user_coinrecord
            ->select('sum(totalcoin) as totalcoin,touid as uid')
            ->where($where)
            ->group('touid')
            ->order('totalcoin desc')
            ->limit($start,$pnum)
            ->fetchAll();
		return $result;
		
	}
	//消费(土豪)榜
	public function consumeList($where='',$p=''){
		 if($p<1){
            $p=1;
        }
        
        // 保证只能查询两页数据
        if($p>3){
            $p=100000000;
        }
        
		$pnum=50;
		$start=($p-1)*$pnum;
		
		$result=\PhalApi\DI()->notorm->user_coinrecord
            ->select('sum(totalcoin) as totalcoin, uid')
            ->where($where)
            ->group('uid')
            ->order('totalcoin desc')
            ->limit($start,$pnum)
            ->fetchAll();
		return $result;
		
	}
	
	//收益(魅力)榜：前三名
	public function getProfittop($where=''){
		$result=\PhalApi\DI()->notorm->user_coinrecord
            ->select('sum(totalcoin) as totalcoin,touid as uid')
            ->where($where)
            ->group('touid')
            ->order('totalcoin desc')
            ->limit(0,3)
            ->fetchAll();
		return $result;		
	}
	
	//消费(土豪)榜：前三名
	public function getConsumetop($where=''){
		$result=\PhalApi\DI()->notorm->user_coinrecord
            ->select('sum(totalcoin) as totalcoin, uid')
            ->where($where)
            ->group('uid')
            ->order('totalcoin desc')
            ->limit(0,3)
            ->fetchAll();
		return $result;
		
	}

}
